package org.jeecg.modules.biz.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.biz.entity.BurnCashOutApply;
import org.jeecg.modules.biz.entity.BurnIncome;
import org.jeecg.modules.biz.entity.BurnIncomeDetail;
import org.jeecg.modules.biz.entity.EventBurn;
import org.jeecg.modules.biz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "燃烧")
@RequestMapping("/burn")
public class EventBurnController {
    @Autowired
    private BurnIncomeService incomeService;
    @Autowired
    private BurnCashOutAppleService cashOutAppleService;
    @Autowired
    private BurnIncomeDetailService incomeDetailService;
    @Autowired
    private EventBurnService burnService;
    @Autowired
    private BurnAmountService amountService;

    @ApiOperation("燃烧申请列表-未审核-管理员可用")
    @RequestMapping(value = "/list/isExamine", method = RequestMethod.GET)
    public Result<IPage<Map<String, Object>>> list(@RequestParam(name = "pageNo", defaultValue = "1")Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10")Integer pageSize){
        Result<IPage<Map<String, Object>>> result = new Result<>();

        Page<Map<String, Object>> page = new Page<>(pageNo, pageSize);
        IPage<Map<String, Object>> pageList = cashOutAppleService.findList4Examine(page);
        result.setResult(pageList);

        return result;
    }

    @ApiOperation("通过审核-管理员可用")
    @RequestMapping(value = "/examine", method = RequestMethod.POST)
    public Result<?> examine(@RequestBody JSONObject jsonObject){

        String address = "41" + String.valueOf(jsonObject.get("address")).substring(2);
        Long amount = Long.valueOf(String.valueOf(jsonObject.get("amount")));
        cashOutAppleService.updateAndFreezingAmount(address, amount);

        return Result.ok();
    }

    @ApiOperation("燃烧提现申请")
    @RequestMapping(value = "/withdraw/apply/{address}", method = RequestMethod.POST)
    public Result<Boolean> cashOutApply(@PathVariable String address){
        Result<Boolean> result = new Result<>();

        BurnIncome burnIncome = incomeService.getOne(new QueryWrapper<BurnIncome>().eq("address", address));
        if(burnIncome != null){
            if(burnIncome.getBalance() - burnIncome.getFreezingAmount() > 5000000){
                QueryWrapper<BurnCashOutApply> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("address", address);
                queryWrapper.eq("is_examine", false);
                BurnCashOutApply cashOutApply = cashOutAppleService.getOne(queryWrapper);
                if(cashOutApply == null){
                    BurnCashOutApply burnCashOutApply = new BurnCashOutApply();
                    burnCashOutApply.setAddress(address);
                    cashOutAppleService.save(burnCashOutApply);
                    result.setResult(true);
                } else {
                    result.error500("已申请提现,请勿重复提交");
                }
            } else{
             result.error500("余额未达到提现要求");
            }
        } else {
            result.error500("未查到燃烧记录");
        }

        return result;
    }

    @ApiOperation("燃烧收益明细")
    @RequestMapping(value = "/income/list/{address}", method = RequestMethod.GET)
    public Result<List<BurnIncomeDetail>> incomeDetailList(@PathVariable String address){
        Result<List<BurnIncomeDetail>> result = new Result<>();

        QueryWrapper<BurnIncomeDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address", address);
        queryWrapper.orderByDesc("timestamp");
        List<BurnIncomeDetail> list = incomeDetailService.list(queryWrapper);
        result.setResult(list);

        return result;
    }

    @ApiOperation("闪兑列表-管理员")
    @RequestMapping(value = "/list/page", method = RequestMethod.GET)
    public Result<IPage<EventBurn>> list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             EventBurn eventBurn,
                                             HttpServletRequest request){
        Result<IPage<EventBurn>> result = new Result<>();

        QueryWrapper<EventBurn> queryWrapper = QueryGenerator.initQueryWrapper(eventBurn, request.getParameterMap());;
        queryWrapper.orderByDesc("timestamp");

        Page<EventBurn> page = new Page<>(pageNo, pageSize);
        IPage<EventBurn> pageList = burnService.page(page, queryWrapper);
        result.setResult(pageList);

        return result;
    }

    @ApiOperation("燃烧事件明细")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<List<EventBurn>> burnList(){
        Result<List<EventBurn>> result = new Result<>();

        QueryWrapper<EventBurn> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("timestamp");
        List<EventBurn> list = burnService.list(queryWrapper);
        result.setResult(list);

        return result;
    }

    @ApiOperation("个人燃烧明细")
    @RequestMapping(value = "/list/{address}", method = RequestMethod.GET)
    public Result<List<EventBurn>> list4Address(@PathVariable String address){
        Result<List<EventBurn>> result = new Result<>();

        QueryWrapper<EventBurn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("caller", address);
        queryWrapper.orderByDesc("timestamp");
        List<EventBurn> list = burnService.list(queryWrapper);
        result.setResult(list);

        return result;
    }

    @ApiOperation("燃烧总量")
    @RequestMapping(value = "/totalAmount", method = RequestMethod.GET)
    public Result<Long> count(){
        Result<Long> result = new Result<>();

        Long totalAmount = amountService.getTotalBurnAmount();
        result.setResult(totalAmount);

        return result;
    }

    @ApiOperation("今日燃烧总量")
    @RequestMapping(value = "/currentAmount", method = RequestMethod.GET)
    public Result<Long> count4Today(){
        Result<Long> result = new Result<>();

        Long totalAmount4Today = burnService.totalAmount4Today();
        result.setResult(totalAmount4Today);

        return result;
    }

    @ApiOperation("可提现收益(余额减去冻结额度)")
    @RequestMapping(value = "/account/balance/{address}", method = RequestMethod.GET)
    public Result<Long> canWithdrawalAmount(@PathVariable String address){
        Result<Long> result = new Result<>();

        Long amount = incomeService.canWithdrawalAmount(address);
        result.setResult(amount);

        return result;
    }

}
